﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace Tiscs.Practices.Web.Services
{
    /// <summary>
    /// 身份验证服务宿主类。
    /// </summary>
    public class AuthenticationServiceHost : ServiceHost
    {
        /// <summary>
        /// 构造方法。
        /// 使用指定的服务类型和服务基地址创建服务宿主。
        /// </summary>
        /// <param name="serviceType">服务类型。</param>
        /// <param name="baseAddresses">服务基地址。</param>
        public AuthenticationServiceHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        /// <summary>
        /// 添加服务默认终结点。
        /// </summary>
        /// <returns>服务默认终结点。</returns>
        public override ReadOnlyCollection<ServiceEndpoint> AddDefaultEndpoints()
        {
            Binding binding = new CustomBinding(new BinaryMessageEncodingBindingElement(), new HttpTransportBindingElement() { AllowCookies = true });
            List<ServiceEndpoint> endpoints = new List<ServiceEndpoint>();
            foreach (ContractDescription contract in ImplementedContracts.Values)
            {
                endpoints.Add(base.AddServiceEndpoint(contract.ConfigurationName, binding, string.Empty));
            }
            return endpoints.AsReadOnly();
        }
    }
}
